package  {

	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.display.Loader;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	import flash.system.Security;
	
	/**
	 * ...
	 * @author Eppic Creative
	 */
	public class GenericLoader extends MovieClip {
		
		private var urlToLoad:String;
		private var loadBar:Sprite;
		private var txtMask:Sprite;
		private var eppicButton:SimpleButton;
		private var barnLoader:Loader;
		private var container:Sprite;
		
		public function GenericLoader() {
			Security.allowDomain('*');
			urlToLoad = "http://www.lititzqualitystructures.com/design_center/design_center.swf";
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}
		
		private function addedToStageHandler(e:Event):void {
			loadBarnSwf();
			initLoadingGraphics();
			
			container = new Sprite();
			addChildAt(container, 0);
		}
		
		private function loadBarnSwf():void {
			barnLoader = new Loader();
			var swfURL:URLRequest = new URLRequest(urlToLoad);
			///var loaderContext:LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain, SecurityDomain.currentDomain);
			
			barnLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
			barnLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, updateLoadBar);
			
			barnLoader.load(swfURL);//, loaderContext);
		}
		
		private function initLoadingGraphics():void {
			txtMask = loadingTxtMask;
			txtMask.scaleX = 0;
			
			loadBar = loadingbar;
			loadBar.scaleX = 0;
			
			eppicButton = eppicBtn;
		}
		
		private function loadComplete(e:Event):void {
			barnLoader.removeEventListener(Event.COMPLETE, loadComplete);
			barnLoader.removeEventListener(ProgressEvent.PROGRESS, updateLoadBar);
			removeChild(loadBar);
			removeChild(eppicButton);
			removeChild(txtMask);
			removeChild(titles);
			removeChild(bg);
			removeChild(barBg);
			removeChild(whtTxt);
			
			container.addChild(barnLoader.content);
		}
		
		private function updateLoadBar(event:ProgressEvent):void {
			var percent:Number = event.bytesLoaded/event.bytesTotal;
			if(loadBar.scaleX < 1) loadBar.scaleX = percent;
			if(txtMask.scaleX < 1) txtMask.scaleX = percent;
		}
		
		private function gotoEppic(e:MouseEvent):void {
			navigateToURL(new URLRequest("http://www.eppic.net/"), "_blank");
		}
		
	}

}